<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>站酷之jsonp代理</title>
    <style>
        *{
              margin: 0;
              padding: 0;
        }
        body,html{
              height: 100%;
              background: #f4f4f4;
              font: 14px/1.5 "PingFang SC","Lantinghei SC","Microsoft YaHei","HanHei SC","Helvetica Neue","Open Sans",Arial,"Hiragino Sans GB","微软雅黑",STHeiti,"WenQuanYi Micro Hei",SimSun,sans-serif;
        }
        .container{
              width: 1150px;
              margin: 20px auto;
        }
        .box{
              border-radius: 4px;
              overflow: hidden;
              float: left;
              margin-right: 20px;
              background: #fff;
              position: relative;
              padding-bottom: 5px;
        }
        .box img{
              width: 210px;
              height: 118px;
        }
        .title{
              padding: 0 10px;
              margin-top: 4px;
              white-space: nowrap;
              text-overflow: ellipsis;
              overflow: hidden;
              width: 190px;
        }
  </style>
</head>
<body>
    <button id="change">换一换</button>
    <div class="container"></div>


    <script>

        // 接口参数
        var pageNo = 2;
        var formId = 379;

        var btn = document.querySelector('#change');
        var box = document.querySelector('.container');

        btn.onclick = () =>{
            // 1.创建script标签并插入到body
            var script = document.createElement('script');
            script.src = `https://api.gogoup.com/p1/data/recommend?type=0&pageNo=${pageNo}&pageSize=5&fromId=${formId}&jsonpcallback=callback&_=${Date.now()}`;
            document.body.appendChild(script);
        }
        
        // 2.设置回调函数接收数据
        function callback(data){
            var data = data.datas;
            var html = '';
            // 遍历数据并渲染
            data.forEach(({image,name})=>{
                html += `
                    <div class="box"> 
                            <img src="${image}" alt=""> 
                            <div class="title">${name}</div>
                    </div>
                `;
            });
            box.innerHTML = html;
            // 参数改变
            pageNo++;
            formId = data[data.length-1].courseId;
        }
    </script>
    
</body>
</html>